#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   BOOT_RequestTransferExit_0x37_test.py
@Time    :   2025/04/10 11:16:25
@Author  :   Yang Liang
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# Standard library imports
import sys
import time

# Third-party imports
import allure
import pytest
import requests
from numpy import byte, bytes_, int16
from pytest_assume.plugin import assume
from sympy import I
from sqlalchemy import false
from collections.abc import Iterable

# Local imports
from commDriver.TS_CAN import *

sys.dont_write_bytecode = True

RequestTransferExit_NRC13 = [
    ("37 01", "7F 37 13"),
    ("37 00 FF", "7F 37 13"),
    ("37 01 FF", "7F 37 13"),
    ("37 03", "7F 37 13"),
    ("37 02", "7F 37 13"),
]

eraseData = "34 00 44 10 00 b8 00 00 00 03 1c"

FlashDriverData = ("36 01 53 30 30 30 30 30 33 35 34 36 30 30 31 00 00 00 "
                 "53 30 30 30 30 30 33 35 34 33 30 30 31 00 00 00 28 b8 00 10 "
                 "98 b9 00 10 e8 32 00 20 49 44 01 24 02 20 49 44 03 24 04 21 "
                 "48 44 05 24 06 21 48 44 07 24 08 22 09 22 47 42 47 43 32 85 "
                 "00 08 02 09 01 70 71 f1 45 42 02 70 6e f1 01 08 03 09 44 42 "
                 "20 85 21 85 04 08 06 09 01 70 65 f1 41 44 04 70 62 f8 24 5c "
                 "ff 03 14 69 5e f1 3f 44 40 70 5b f8 08 08 09 09 01 70 57 f1 "
                 "3c 44 04 70 54 f0 00 08 02 09 01 70 50 f1 35 42 02 70 4d f1 "
                 "05 08 07 09 37 42 20 85 21 85 00 08 02 09 01 70 44 f1 2f 42 "
                 "02 70 41 f1 08 08 09 09 01 70 3d f1 22 5c ff 03 90 d4 39 f1 "
                 "04 08 06 09 01 70 35 f1 29 44 04 70 32 f8 24 5c ff 03 21 d5 "
                 "2e f1 27 44 40 70 2b f8 28 42 20 85 32 82 31 70 26 f1 27 42 "
                 "20 5c fb 07 20 85 32 82 30 70 1f f1 24 42 25 40 20 85 25 44 "
                 "25 43 13 82 0b 54 04 04 41 30 02 f0 fa 07 40 38 12 f0 04 08 "
                 "11 10 8a 7a 01 64 04 20 06 20 08 08 01 66 08 20 09 20 c7 f8 "
                 "00 10 0e 42 0e 43 32 85 00 10 05 04 0c 10 0b 42 0b 43 32 85 "
                 "00 20 01 20 02 20 03 20 04 20 05 20 06 20 07 20 e8 2a 1d 5c "
                 "30 43 65 27 04 03 04 81 de dc 96 78 07 14 15 92 07 00 af 50 "
                 "00 01 20 40 88 66 5a 5a 28 01 20 40 00 00 08 00 00 02 00 00 "
                 "00 00 00 00 2c 01 20 40 1c 01 20 40 08 01 20 40 0c 01 20 40 "
                 "01 00 af 50 88 13 00 00 20 01 20 40 25 5d e0 33 00 20 4f 44 "
                 "01 24 02 20 4e 44 03 24 04 21 4e 44 05 24 06 21 4d 44 07 24 "
                 "08 22 09 22 0a 23 0b 23 4b 42 4c 43 32 85 04 08 06 09 01 70 "
                 "74 f1 4a 44 04 70 71 f8 24 5c 07 00 14 69 6d f1 47 44 40 70 "
                 "6a f8 08 08 09 09 01 70 66 f1 45 44 04 70 63 f0 00 08 02 09 01 "
                 "70 5f f1 42 42 02 70 5c f1 01 08 03 09 41 42 20 85 21 85 00 "
                 "08 02 09 01 70 53 f1 3c 42 02 70 50 f1 05 08 07 09 3c 42 20 "
                 "85 21 85 00 08 02 09 01 70 47 f1 36 42 02 70 44 f1 04 08 06 09 "
                 "01 70 40 f1 08 08 09 09 01 70 3c f1 27 10 02 69 39 f1 2b 42 "
                 "13 4c 32 80 2b 14 32 70 33 f1 2f 42 20 5c f5 07 21 5c f5 07 20 "
                 "85 32 82 31 70 05 f0 24 42 22 40 20 85 26 04 04 08 06 09 01 70 "
                 "22 f1 08 09 09 0a 21 70 1e f1 0a 0a 0b 0b 23 70 1a f1 32 72 03 "
                 "73 32 72 03 73 18 30 21 43 22 44 34 85 22 43 22 45 2a 50 03 04 "
                 "31 30 07 f0 23 5c 07 00 31 69 08 f1 10 38 ed f1 11 42 11 43 32 "
                 "85 00 10 05 04 0c 10 0e 42 0e 43 32 85 00 20 01 20 02 20 03 20 "
                 "04 20 05 20 06 20 07 20 08 20 09 20 0a 20 0b 20 e0 2b 05 5d 1d "
                 "5c 00 00 30 43 65 27 04 03 04 81 de dc 96 78 07 14 15 92 07 00 "
                 "af 50 00 01 20 40 00 00 08 00 00 02 00 00 00 00 00 00 88 66 5a "
                 "5a 28 01 20 40 2c 01 20 40 08 01 20 40 0c 01 20 40 01 00 af 50 "
                 "88 13 00 00 20 01 20 40")

def EraseAndTransfer(Driver, iStep):
    iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, eraseData, '74', 2, iStep, True)
    iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, FlashDriverData, '76', 0, iStep, False)
    return iStep

class Test_BOOT_RequestTransferExit_Phy(object):
    @allure.severity('minor')
    @allure.title('物理寻址-编程会话-请求传输退出-0x37-NRC24')
    @allure.description('测试物理寻址编程会话下请求传输退出0x37的NRC24')
    def test_RequestTransferExit_NRC24_Programming_Phy(self, Driver, action):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '37', '7F 37 24', 2, iStep, True)


    # @allure.severity('minor')
    # @allure.title('物理寻址-编程会话-请求传输退出-0x37-NRC31')
    # @allure.description('测试物理寻址编程会话下请求传输退出0x37的NRC31')
    # def test_RequestTransferExit_NRC31_Programming_Phy(self, Driver, action):
    #     iStep = 1
    #     iStep = EraseAndTransfer(Driver, iStep)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, "37", "7F 37 31", 0, iStep, False)


    ids = [f"Request {req}" for req, resp in RequestTransferExit_NRC13]
    @allure.severity('minor')
    @allure.title('物理寻址-编程会话-请求传输退出-0x37-NRC13')
    @allure.description('测试物理寻址编程会话下请求传输退出0x37的NRC13')
    @pytest.mark.parametrize('req,resp', RequestTransferExit_NRC13, ids=ids)
    def test_RequestTransferExit_NRC13_Programming_Phy(self, Driver, action, req, resp):
        iStep = 1
        iStep = EraseAndTransfer(Driver, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)

    @allure.severity('minor')
    @allure.title('物理寻址-默认会话-请求传输退出-0x37-NRC11')
    @allure.description('测试物理寻址默认会话下请求传输退出0x37的NRC11')
    def test_RequestTransferExit_NRC11_Default_Phy(self, Driver, action):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, "37", "7F 37 11", 1, iStep, False)

    @allure.severity('minor')
    @allure.title('物理寻址-扩展会话-请求传输退出-0x37-NRC11')
    @allure.description('测试物理寻址扩展会话下请求传输退出0x37的NRC11')
    def test_RequestTransferExit_NRC11_Extended_Phy(self, Driver, action):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, "37", "7F 37 11", 3, iStep, True)

    @allure.severity('minor')
    @allure.title('物理寻址-编程会话-请求传输退出-0x37-NRC72')
    @allure.description('测试物理寻址编程会话下请求传输退出0x37的NRC72')
    def test_RequestTransferExit_NRC72_Programming_Phy(self, Driver, action):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '34 00 44 10 00 b8 00 00 00 00 64', '74 20 04 02', 2, iStep, True)
        req = '36 01' + '00' * 20
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver,req, '76', 0,iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, "37", "7F 37 72", 0, iStep, False)

class Test_BOOT_RequestTransferExit_Func(object):
    ids = [f"Request {req}" for req, resp in RequestTransferExit_NRC13]
    @allure.severity('minor')
    @allure.title('功能寻址-编程会话-请求传输退出-0x37-无应答')
    @allure.description('测试功能寻址编程会话下请求传输退出0x37的无应答')
    @pytest.mark.parametrize('req,resp', RequestTransferExit_NRC13, ids=ids)
    def test_RequestTransferExit_NRC_No_Resp_Func(self, Driver, action, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, "", 2, iStep, True, 'UDSFun')
     
         

    
     
     